﻿if (typeof(Admin) == 'undefined')
    Admin = {};

Admin.Brand = {
    _pageMax: function () { return $("#pageMax").val() },
    _pageIndex: function () { return $("#pageIndex").val() },
    _options: {
        id: 'gridList',
        url: function () {
            var ret = "Service/ListBrand.ashx?key=e"

            var pm = Admin.Brand._pageMax();
            var pi = Admin.Brand._pageIndex();

            if (pm.length > 0)
                ret += "&pm=" + pm;
            if (pi.length > 0)
                ret += "&pi=" + pi;
            return ret;
        }
    },
    ManipulateData: function (options) {
        var obj = $("#" + options.id + " > tbody");
        var data = new TIO().ajax(options.url());
        var result = "";
        for (i = 0; i < data.length; i++) {
            var item = data[i];
            result += $.Format(_format, item.BrandId, item.BrandName, item.BrandLogo);
        }
        obj.html(result);
    },
    UpdatePhotosLink: function () {
        var link1 = $("#upload1").attr("filename");
        $("input[id$='logoURL']").val(link1);
    },
    Edit: function (bid) {
        var height = window.screen.height - 130;
        window.open("EditBrand.aspx?bi=" + bid, "_blank", "resizable=1,width=770,height=" + height);
    },
    Delete: function (bid) {
        var data = new TIO().ajax("Service/DeleteBrand.ashx?bi=" + bid);
        this.ManipulateData(this._options);
    },
    InitEvent: function () {
        var thisObj = this;
        $("#pageIndex").keyup(function (e) {
            var key = e.charCode || e.keyCode || 0;
            if (key == 48 || key == 49 || key == 50 || key == 51 || key == 52 || key == 53 ||
                key == 54 || key == 55 || key == 56 || key == 57) {
                thisObj.ManipulateData(thisObj._options);
            }
        });
        $("#pageMax").change(function () {
            thisObj.ManipulateData(thisObj._options);
        });
    },
    InitUploader: function () {
        var setting = { action: 'Service/UploadImageBrand.ashx' };
        Admin.Core.Uploader('upload1', setting);
    },
    Init: function () {
        this.InitEvent();
        this.InitUploader();
        this.ManipulateData(this._options);
    }
}

$(function () {
    Admin.Brand.Init();
});

_format = "<tr>" + 
"<td class='a-center'>{0}</td>" +
"<td><a href='#'>{1}</a></td>" +
"<td>{2}</td>" +
"<td>" +
    "<a pid='{0}' href='#'><img src='../../img/icons/user.png' title='Show detail' width='16' height='16' /></a>" +
    "<a pid='{0}' href='javascript:Admin.Brand.Edit({0});'><img src='../../img/icons/user_edit.png' title='Edit brand' width='16' height='16' /></a>" +
    "<a pid='{0}' href='javascript:Admin.Brand.Delete({0});'><img src='../../img/icons/user_delete.png' title='Delete brand' width='16' height='16' /></a>" +
"</td>" +
"</tr>";